﻿using System.Windows;
using System.Windows.Controls;

namespace Silverlight.InputCtrl
{
    /// <summary>
    /// HyperlinkButton Control integrate with the validator Manager Control 
    /// </summary>
    public class HyperlinkButtonValidator : HyperlinkButton
    {
        /// <summary>
        /// Name of the group to validate
        /// </summary>
        /// <remarks>
        /// Default value is empty
        /// </remarks>
        public string GroupValidator
        {
            get { return (string)GetValue(GroupValidatorProperty); }
            set { SetValue(GroupValidatorProperty, value); }
        }

        public static readonly DependencyProperty GroupValidatorProperty =
            DependencyProperty.Register("GroupValidator", typeof(string), typeof(HyperlinkButtonValidator), new PropertyMetadata(""));


        /// <summary>
        /// Validate Always Empty Name Group
        /// </summary>
        /// <remarks>
        /// Default value is false
        /// </remarks>
        public bool ValidateAllGroups
        {
            get { return (bool)GetValue(ValidateAllGroupsProperty); }
            set { SetValue(ValidateAllGroupsProperty, value); }
        }

        public static readonly DependencyProperty ValidateAllGroupsProperty =
            DependencyProperty.Register("ValidateAllGroups", typeof(bool), typeof(ButtonValidator), new PropertyMetadata(false));

        /// <summary>
        /// Execute validatation before click .
        /// Only fire event click if the validation no result false.
        /// </summary>
        /// <remarks>
        /// If not found Validator context onClick is fire normally.
        /// </remarks>
        protected override void OnClick()
        {
            bool result = true;
            if (Application.Current.Resources.Contains("__ValidatorManager.Context"))
            {
                if (ValidateAllGroups)
                {
                    result = ValidatorManager.ValidateAll();
                }
                else
                {
                    result = ValidatorManager.Validate(GroupValidator);
                }
            }

            if (result)
            {
                base.OnClick();
            }
        }

    }
}